import {
	queryOrderPayStatus,
} from "@/api/payment/index.js"


module.exports = {

	data() {
		return {
			timer: null,
			pagePath: '',
		}
	},
	onLoad() {
		this.pagePath = this.$mp.page.route
	},

	methods: {
		queryOrderPayStatus(option) {
			let {
				success,
				fail,
				complete,
			} = option
			if (this.$mp.page.route !== this.pagePath) return
			let _this = this
			if (this.timer !== null) {
				clearTimeout(this.timer)
			}
			queryOrderPayStatus({
				orderNo: this.orderNo
			}).then(res => {
				console.log(res, "res----------")
				success&&success(res)
				complete&&complete(res)
				uni.reLaunch({
					url: "/pages/payment/success"
				})
				clearTimeout(this.timer)
			}).catch((err) => {
				console.log(err, "err----------")
				if (err.code == 2020) {
					this.timer = setTimeout(() => {
						_this.queryOrderPayStatus({complete,success,fail})
					}, 1000)
				} else {
					uni.hideLoading();
					fail&&fail(err)
					complete&&complete()
					uni.reLaunch({
						url: "/pages/payment/error"
					})
					clearTimeout(this.timer)
				}
			})

		}
	},
	onUnload() {
		clearTimeout(this.timer)
	},

}